import requests
from lxml import etree

# from app.blueprints import app

headers = {
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/'
                  '88.0.4324.182 Safari/537.36 Edg/88.0.705.74',
}


# https://cn.bing.com/search?q=%E8%BF%91%E6%9C%9F%E6%96%B0%E9%97%BB
# https://www.baidu.com/s?wd=%E7%AD%89%E7%AD%89
def query(engine: str, key_world: str, params: dict = None) -> str:
    """"
        设置要搜索的引擎 及查询参数
        返回 page_text 网页文本页面
    """
    if params:
        # params 参数不为空
        search_engines = {
            "bing": {"url": "https://cn.bing.com/search", "params": params},
            "baidu": {"url": "https://www.baidu.com/s", "params": params},
        }

    else:
        search_engines = {
            "bing": {"url": "https://cn.bing.com/search", "params": {"q": key_world}},
            "baidu": {"url": "https://www.baidu.com/s", "params": {"wd": key_world}},
        }

    search_engine = search_engines["bing"]
    resp = requests.get(url=search_engine["url"], params=search_engine["params"], headers=headers)
    # print(resp.url)
    # 改编码
    resp.encoding = 'utf-8'
    _page_text = resp.text
    # app.logger.info('请求路径 :  %s ', resp.url)

    return _page_text


def turn_tree(page_text: str):
    """

    :type page_text: object
    """
    tree = etree.HTML(page_text)
    # 去掉 id sb_form
    sb_form = tree.xpath("//form[@id='sb_form']")[0]
    print(sb_form)
    sb_form = ""

    print(sb_form)
    return tree


if __name__ == "__main__":
    page_text = query(engine="bing", key_world="刘德华")
    # print(page_text)
    tree = turn_tree(page_text)
    print(tree)
    head = tree.xpath("//head")[0]
    head.xpath("//title")[0] = ""
    print(etree.tostring(head).decode())

